<?php

use models\Business\Order;
use models\Business\RefundInfo;
use models\Exception\Business\OrderException;
use models\Exception\Business\RefundinfoException;
use Yaf\Registry;


class OrderController extends ApiBaseController
{

    /**
     * @SWG\Get(
     *     path="/admin/order/list",
     *     tags={"1688订单"},
     *     summary="获取1688订单列表数据接口",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page",
     *          description="页码",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="page_size",
     *          description="每页显示条数",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sort",
     *          description="排序方式 可选值: desc|asc",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="order",
     *          description="排序字段 可选值: create_at",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="member_id",
     *          description="用户id",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sn",
     *          description="订单号 模糊查询",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="trade_order_sn",
     *          description="1688订单号 模糊查询",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="type",
     *          description="订单类型 可选值: 0为购买,1为兑换",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="product_id",
     *          description="产品id",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="pay_type",
     *          description="支付通道 可选值: 0为支付宝,1为微信,2为余额",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="stats",
     *          description="状态 可选值:1为待发货,2为已发货,3为确认收货",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="refund_stats",
     *          description="退货退款状态 可选值:0为不退款不退货,1为发起退款,2为发起退货,3为退款成功,4为退货成功,5为退款失败,6为退货失败",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="start_created_date",
     *          description="下单时间",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="end_created_date",
     *          description="下单时间",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="start_refunded_date",
     *          description="发起退款时间",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="end_refunded_date",
     *          description="发起退款时间",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="start_returned_date",
     *          description="发起退货时间",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="end_returned_date",
     *          description="发起退货时间",
     *          in="query",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="query",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/orderListData")
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function listAction()
    {
        $parameters = Registry::get('parameters');
        $order_list = Order::getOrderList($parameters);

        return $this->_responseJson($order_list);
    }


    /**
     * @SWG\Put(
     *     path="/admin/order/{id}/edit",
     *     tags={"1688订单"},
     *     summary="编辑1688订单接口",
     *     description="",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sn",
     *          description="/admin/Order/list接口返回的sn字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="stats",
     *          description="状态(0待支付,1为待发货,2为已发货,3为确认收货,当type为1时,该字段默认值为1)",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="refund_stats",
     *          description="退货退款状态(0为不退款不退货,1为发起退款,2为发起退货,3为退款成功,4为退货成功,5为退款失败,6为退货失败,默认为0,当type为1时,该字段固定值0.当stats为1时方可发起退款,当stats为3时方可发起退货)",
     *          in="formData",
     *          required=false,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="sign",
     *          description="签名",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功",
     *         @SWG\Schema(type="object", ref="#/definitions/orderSingleData")
     *     ),
     *     @SWG\Response(
     *         response="422",
     *         description="编辑失败"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function editAction()
    {
        $parameters = Registry::get('parameters');
        $update_res = Order::edit($parameters);
        return $this->_responseJson(['data' => $update_res]);
    }

    /**
     * @SWG\Get(
     *     path="/admin/order/{order_sn}/queryRefundReturn",
     *     tags={"1688订单"},
     *     summary="获取查询退货、退款订单订单详情",
     *     description="
     * timestamp 必须参数 当前时间戳",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="order_sn",
     *          description="/admin/order/list接口返回的sn字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function queryRefundReturnAction()
    {
        // 查询退货、退款订单订单详情
        // 调用1688的"查询退款单详情"接口
        // 若调用失败，记录错误信息，并提示前端接口调用失败联系技术
        // 反之，将结果返回给客户端
    }

    /**
     *
     * @SWG\Get(
     *     path="/admin/order/{order_sn}/refundReturnRecord",
     *     tags={"1688订单"},
     *     summary="买卖双方退款操作记录",
     *     description="
     * timestamp 必须参数 当前时间戳",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="order_sn",
     *          description="/admin/order/list接口返回的sn字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function refundReturnRecordAction()
    {
        // 买卖双方退款操作记录
        // 调用1688的"买卖双方退款操作记录"接口
        // 若调用失败，记录错误信息，并提示前端接口调用失败联系技术
        // 反之，将结果返回给客户端
    }
    /**
     *
     * @SWG\Get(
     *     path="/admin/order/{order_sn}/refundInfo",
     *     tags={"1688订单"},
     *     summary="获取某个订单退款退货原因信息",
     *     description="
     * timestamp 必须参数 当前时间戳",
     *     @SWG\Parameter(
     *          name="Authorization",
     *          description="Authorization 登录接口返回的jwt字段的值",
     *          in="header",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="order_sn",
     *          description="/admin/order/list接口返回的sn字段的值",
     *          in="path",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Parameter(
     *          name="secret",
     *          description="加密字符串",
     *          in="formData",
     *          required=true,
     *          type="string"
     *     ),
     *     @SWG\Response(
     *         response="200",
     *         description="请求成功"
     *     ),
     *     @SWG\Response(
     *         response="400",
     *         description="签名不存在或无效签名"
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="jwt无效或过期，需要登录"
     *     ),
     *     @SWG\Response(
     *         response="403",
     *         description="无权访问"
     *     ),
     *     @SWG\Response(
     *         response="404",
     *         description="找不到数据"
     *     )
     * )
     */
    public function infoAction()
    {
        $parameters = Registry::get('parameters');

        $refundInfo = RefundInfo::getRefundInfoBySn($parameters['id']);

        if (empty($refundInfo)) {
            throw new OrderException(RefundinfoException::REFUNDINFO_IS_NOT_EXISTS, RefundinfoException::REFUNDINFO_IS_NOT_EXISTS_NO);

        }
        return $this->_responseJson(['data' => $refundInfo]);
    }
}
